import axios from 'axios'
import { Message } from 'element-ui'
import router from '../router'


axios.interceptors.request.use(config => {
// Do something before request is sent
if(window.sessionStorage.getItem('tookenStr')){
   // 如果存在请求携带这个token
    config.headers['Authorixstion'] = window.sessionStorage.getItem('tookenStr')
}
return config;
},error => {
console.log(error)
});

//又一个响应的拦截器,统一响应失败或者其他可以统一,
axios.interceptors.response.use(success => {
    //表示成功钓到接口
    //业务逻辑错误
    if(success.status && success.status == 200){
        if(success.data.code==500||success.data.code==403||success.data.code==401){
            Message.error({message:success.data.message});
            return;
        }
        if(success.data.message){
            Message.success({message:success.data.message});
            
        }
        return success.data;
    }
},error => {
    //完全就错了
    if(error.response.code==504||error.response.code==404){
        Message.error({message:'服务器被吃'});
    }else if(error.response.code==403){
        Message.error({message:'全县不足，练习管理员'})
    }else if(error.response.code==401){
        Message.error({message:'未登录'})
        router.replace('/');
    }else{
        if(error.response.data.message){
            Message.error({message:error.response.data.message})
        }else{
            Message.error({message:'未知错误'})
        }
    }
    return ;
})

//let base ='';
//传送json格式post请求

/* const baseAjax = {
    postRequest :()=>{
        console.log('请求到了');
        
    } 
}*/
/* const postRequest = {
    postajax:()=>{
        console.log('请求到了');
        
    } 

} */

export   const postRequest = ()=> {
    console.log('请求到了');

}

export   const putRequest = ()=> {
    console.log('请求到了put');

}

